using DevExpress.XtraEditors;
using Newtonsoft.Json;
using System;
using System.Net.Http;
using System.Text;
using System.Windows.Forms;

namespace BW.Mes.WinForms.BasicData
{
    public partial class UpdateWorkshopForm : XtraForm
    {
        private readonly WorkshopForm.WorkShopModel _workshop;
        private readonly string _workshopApiBaseUrl = "http://120.26.197.100:8022";

        public UpdateWorkshopForm(WorkshopForm.WorkShopModel workshop)
        {
            InitializeComponent();
            _workshop = workshop;
            LoadWorkshopData();
        }

        private void LoadWorkshopData()
        {
            if (_workshop != null)
            {
                textBoxCode.Text = _workshop.WorkshopCode;
                textBoxName.Text = _workshop.WorkshopName;
                textBoxDesc.Text = _workshop.WorkshopMsg;
            }
        }

        private async void btnOK_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(textBoxCode.Text) || string.IsNullOrWhiteSpace(textBoxName.Text))
            {
                XtraMessageBox.Show("车间编号和车间名称不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            var model = new
            {
                Id = _workshop.Id,
                WorkshopCode = textBoxCode.Text.Trim(),
                WorkshopName = textBoxName.Text.Trim(),
                WorkshopMsg = textBoxDesc.Text.Trim(),
                UpdatedAt = DateTime.Now
            };

            try
            {
                var handler = new HttpClientHandler
                {
                    ServerCertificateCustomValidationCallback = (req, cert, chain, errors) => true
                };

                using (var client = new HttpClient(handler))
                {
                    client.BaseAddress = new Uri(_workshopApiBaseUrl);
                    var json = JsonConvert.SerializeObject(model);
                    var content = new StringContent(json, Encoding.UTF8, "application/json");
                    var response = await client.PutAsync("/T2/Workshop/UpdateWorkshop", content);
                    
                    if (response.IsSuccessStatusCode)
                    {
                        XtraMessageBox.Show("修改成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.DialogResult = DialogResult.OK;
                        this.Close();
                    }
                    else
                    {
                        string errorContent = await response.Content.ReadAsStringAsync();
                        XtraMessageBox.Show($"修改失败：{response.ReasonPhrase} - {errorContent}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show($"修改失败：{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void UpdateWorkshopForm_Load(object sender, EventArgs e)
        {
            LoadWorkshopData();
        }

        private void panelControl1_Paint(object sender, PaintEventArgs e)
        {

        }
    }
} 